﻿using System;
using System.Collections.Generic;
using System.Linq;
using DLM.Domain.Model;
using DLM.Domain.Repositories;
using DLM.Infrastructure.UnitOfWork;
using DLM.Infrastructure.Domain;
using DLM.Domain.DTO;
using Webdiyer.WebControls.Mvc;

namespace DLM.Data.EF.CodeFirst.Repositories
{
    public class ModalityRepository : Repository<Modality, int>, IModalityRepository
    {
        public ModalityRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork)
        { }

        public IEnumerable<Domain.DTO.ModalityDTO> FindModalityDTO(int partyType,int pageIndex,int pageSize)
        {
            if (partyType < 0)
            {

                throw new ArgumentOutOfRangeException("Paarty type");
            }
            var result = from m in this.ActiveContext.Modality.AsNoTracking()
                         join t in this.ActiveContext.Type.AsNoTracking()
                         on m.PID equals t.PartyType
                         where t.PartyType == partyType
                         select new ModalityDTO
                         {
                             MID = m.MID,
                             ModalityName = m.Name,
                             Name = t.Name,
                             PartyType = t.PartyType,
                             PID = t.PID
                         };
            return result.OrderByDescending(m=>m.MID).ToPagedList(pageIndex, pageSize);
        }

        public new System.Collections.IEnumerator GetEnumerator()
        {
            throw new NotImplementedException();
        }

        public IEnumerable<ModalityDTO> FindModalityDTO(int pageInde, int pageSize)
        {
            var result = from m in this.ActiveContext.Modality.AsNoTracking()
                         join t in this.ActiveContext.Type.AsNoTracking()
                         on m.PID equals t.PartyType
                         select new ModalityDTO
                         {
                             MID = m.MID,
                             ModalityName = m.Name,
                             Name = t.Name,
                             PartyType = t.PartyType,
                             PID = t.PID
                         };
            return result.OrderByDescending(m=>m.MID).ToPagedList(pageInde, pageSize);
        }
    }
}
